package com.zhousb.deviceManager.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhousb.deviceManager.mapper.KiAssetMapper;
import com.zhousb.deviceManager.mapper.KiInventoryDetailMapper;
import com.zhousb.deviceManager.mapper.KiInventoryMapper;
import com.zhousb.deviceManager.model.bo.InventorySubmitBo;
import com.zhousb.deviceManager.model.dto.AssetDto;
import com.zhousb.deviceManager.model.entity.KiAsset;
import com.zhousb.deviceManager.model.entity.KiAssetLifecycle;
import com.zhousb.deviceManager.model.entity.KiInventory;
import com.zhousb.deviceManager.model.entity.KiInventoryDetail;
import com.zhousb.deviceManager.security.JwtUser;
import com.zhousb.deviceManager.service.AssetLifecycleService;
import com.zhousb.deviceManager.service.InventoryService;
import com.zhousb.deviceManager.utils.AuthUtils;
import com.zhousb.deviceManager.utils.IdGenerator;
import com.zhousb.deviceManager.utils.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhousb
 * @date 2024/10/31 11:13
 * @description
 */
@Service
public class InventoryServiceImpl implements InventoryService {

    @Resource
    private KiInventoryMapper kiInventoryMapper;
    @Resource
    private KiInventoryDetailMapper kiInventoryDetailMapper;
    @Resource
    private KiAssetMapper kiAssetMapper;
    @Resource
    private AuthUtils authUtils;
    @Resource
    private AssetLifecycleService assetLifecycleService;

    @Override
    public boolean submit(InventorySubmitBo bo) {
        JwtUser user = authUtils.getUser();

        Long inventoryId = IdGenerator.nextId();
        KiInventory kiInventory = new KiInventory();
        kiInventory.setId(inventoryId);
        kiInventory.setInventoryCode("PD" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMddHHmmss"));
        kiInventoryMapper.insert(kiInventory);
        List<AssetDto> assetList = bo.getList();
        List<KiAssetLifecycle> lifecycleList = new ArrayList<>();
        List<KiInventoryDetail> detailList = assetList.stream().map(s -> {
            KiInventoryDetail detail = new KiInventoryDetail();
            detail.setId(IdGenerator.nextId());
            detail.setEpc(s.getAssetEpc());
            detail.setStatus(s.getInventoryStatus());

            if(!s.getInventoryStatus().equals("0")){
                LambdaUpdateWrapper<KiAsset> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(KiAsset::getAssetEpc, s.getAssetEpc())
                        .set(KiAsset::getInventoryStatus, s.getInventoryStatus());
                kiAssetMapper.update(null, updateWrapper);

                KiAssetLifecycle lifecycle = new KiAssetLifecycle();
                lifecycle.setAssetId(Long.valueOf(s.getId()));
                lifecycle.setEventType("盘点");
                lifecycle.setEventContent(s.getInventoryStatus().equals("1")?"盘点在库":"盘点在库（多盘）");
                lifecycle.setEventOperator(user.getUserId());
                lifecycleList.add(lifecycle);
            }



            return detail;
        }).toList();
        kiInventoryDetailMapper.batchInsert(inventoryId, user.getRealName(), detailList);

        assetLifecycleService.batchInsert(lifecycleList,100);
        return false;
    }
}
